#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'


from .vendor.singleton import  singleton
from PySide import QtGui, QtCore

#@singleton
class TrayIcon(QtGui.QSystemTrayIcon):
    clicked = QtCore.Signal()
    double_clicked = QtCore.Signal()
    right_clicked = QtCore.Signal()
    def __init__(self, parent):
        super(TrayIcon, self).__init__(parent)
        self._parent = parent
        icon = QtGui.QIcon(":/icons/ver_logo.png")
        self.setIcon(icon)
        self.setToolTip("Versatile")
        # connect up signal handlers
        #self.activated.connect(self.__activated)
        self.create_context_menu()
        self.activated.connect(self.parent_show)

    def __activated(self, reason):
        if reason == QtGui.QSystemTrayIcon.Trigger:
            self.clicked.emit()
        elif reason == QtGui.QSystemTrayIcon.DoubleClick:
            self.double_clicked.emit()
        elif reason == QtGui.QSystemTrayIcon.Context:
            self.right_clicked.emit()

    def parent_show(self, arg):
        if arg in [QtGui.QSystemTrayIcon.DoubleClick, QtGui.QSystemTrayIcon.Trigger]:
            self._parent.show()
            self._parent.showNormal()

    def show_win(self):
        self.show()
        self._parent.show()

    def create_context_menu(self):
        # create actions
        self.minimizeAction = QtGui.QAction(u"最小化", self, triggered=self._parent.hide)
        #self.maximizeAction = QtGui.QAction("Ma&ximize", self, triggered=self._parent.showMaximized)
        self.restoreAction = QtGui.QAction(u"还原", self, triggered=self._parent.showNormal)
        self.quitAction = QtGui.QAction(u"退出", self, triggered=QtGui.qApp.quit)
        # create menu
        self.trayIconMenu = QtGui.QMenu()
        self.trayIconMenu.addAction(self.minimizeAction)
        #self.trayIconMenu.addAction(self.maximizeAction)
        self.trayIconMenu.addAction(self.restoreAction)
        self.trayIconMenu.addSeparator()
        self.trayIconMenu.addAction(self.quitAction)
        # set context menu
        self.setContextMenu(self.trayIconMenu)